package com.taoyibao.hongcanting.ui.mine.activity;

import android.content.Context;
import android.content.Intent;

import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.ApiConstant;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.OrderEatTypeMode;
import com.taoyibao.hongcanting.model.OrderInfoModel;
import com.taoyibao.hongcanting.model.SanFangBaseModel;
import com.taoyibao.hongcanting.ui.mine.delegate.OrderDetailDelegate;
import com.taoyibao.hongcanting.utils.DataUtils;

import java.util.List;

/**
 * @author TangAnna
 * @description: 订单详情
 * @date :${DATA} 14:09
 */
public class OrderDetailActivity extends BaseActivityPresenter<OrderDetailDelegate> {
    private String mOrderId;
    private String memberId;

    @Override
    protected Class<OrderDetailDelegate> getDelegateClass() {
        return OrderDetailDelegate.class;
    }

    /**
     * @param context
     * @param orderId     订单ID
     * @param memberId
     * @param orderStatus 订单状态
     */
    public static void open(Context context, String orderId, String memberId, int orderStatus) {
        Intent intent = new Intent(context, OrderDetailActivity.class);
        intent.putExtra(CodeConstant.ORDER_ID, orderId);
        intent.putExtra(CodeConstant.MEMBER_ID, memberId);
        intent.putExtra(CodeConstant.STATUS, orderStatus);
        context.startActivity(intent);
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        mOrderId = getIntent().getStringExtra(CodeConstant.ORDER_ID);
        memberId = getIntent().getStringExtra(CodeConstant.MEMBER_ID);
        int orderStatus = getIntent().getIntExtra(CodeConstant.STATUS, 0);
//        if (orderStatus == CodeConstant.ORDER_STATUS_COMPLETED) {//已完成
//            getCompleteOrderInfo();
//        } else if (orderStatus == CodeConstant.ORDER_STATUS_NOPAY) {//待支付
//            getEatType();
//        }
        getEatType();
    }

    /**
     * 获取桌位信息判断未支付订单时外带还是堂食
     */
    public void getEatType() {
        JsonApiWrapper.getApiService().orderEatType(SpUtils.getToken(), mOrderId, CodeConstant.SELECT_ORDERID_EAT_TYPE, null, null)
                .compose(new DefaultTransformer<CompanyBaseModel<List<OrderEatTypeMode>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<OrderEatTypeMode>>>(this, false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<OrderEatTypeMode>> companyBaseModel) {
                        if (companyBaseModel.code == NetCode.HTTP_200 && companyBaseModel.data != null && companyBaseModel.data.size() > 0) {
                            OrderEatTypeMode orderEatTypeMode = companyBaseModel.data.get(0);
                            if (orderEatTypeMode.eatType == CodeConstant.EAT_TYPE_TANGSHI) {//堂食
                                getFoodOrderInfo(companyBaseModel.data.get(0).deskId);
                            } else if (orderEatTypeMode.eatType == CodeConstant.EAT_TYPE_WAIDAI) {//外带
                                getTakeoutOrderInfo();
                            }
                        }
                    }
                });
    }


    /**
     * 获取外带订单信息
     */
    private void getTakeoutOrderInfo() {
        JsonApiWrapper.getApiService().getOrderInfo(ApiConstant.MASTER_ID, DataUtils.getKey(), ApiConstant.BRANCH_ID, memberId, mOrderId)
                .compose(new DefaultTransformer<SanFangBaseModel<OrderInfoModel>>())
                .subscribe(new ProgressSubscriber<SanFangBaseModel<OrderInfoModel>>(this, true) {
                    @Override
                    public void onNext(SanFangBaseModel<OrderInfoModel> result) {
                        if (result.Code == NetCode.HTTP_200 && result.Data != null) {
                            viewDelegate.setData(result);
                        } else {
                            super.onNext(result);
                        }
                    }
                });
    }

    /**
     * 获取堂食订单详情
     */
    public void getFoodOrderInfo(String deskId) {
        JsonApiWrapper.getApiService().getDeskOrderInfo(ApiConstant.MASTER_ID, DataUtils.getKey(), ApiConstant.BRANCH_ID, deskId)
                .compose(new DefaultTransformer<SanFangBaseModel<OrderInfoModel>>())
                .subscribe(new ProgressSubscriber<SanFangBaseModel<OrderInfoModel>>(this, true) {
                    @Override
                    public void onNext(SanFangBaseModel<OrderInfoModel> result) {
                        if (result.Code == NetCode.HTTP_200 && result.Data != null) {
                            viewDelegate.setData(result);
                        } else {
                            super.onNext(result);
                        }
                    }
                });
    }

    /**
     * 获取已完成的订单信息
     */
    public void getCompleteOrderInfo() {
        JsonApiWrapper.getApiService().getOrderInfoComplete(ApiConstant.MASTER_ID, DataUtils.getKey(), ApiConstant.BRANCH_ID, memberId, mOrderId)
                .compose(new DefaultTransformer<SanFangBaseModel<OrderInfoModel>>())
                .subscribe(new ProgressSubscriber<SanFangBaseModel<OrderInfoModel>>(this, true) {
                    @Override
                    public void onNext(SanFangBaseModel<OrderInfoModel> result) {
                        if (result.Code == NetCode.HTTP_200 && result.Data != null) {
                            viewDelegate.setData(result);
                        } else {
                            super.onNext(result);
                        }
                    }
                });
    }
}
